<template>
  <div class="app-container bg">
    <div class="box-container">
      <div
        class="mb15"
        style="border-bottom: 1px solid #ddd; margin-bottom: 10px"
      >
        <el-radio-group
          v-model="showType"
          @change="showTypeChange"
          class="custom-radio-group"
        >
          <el-radio-button width="100" label="1">科室</el-radio-button>
          <el-radio-button label="2">学员</el-radio-button>
        </el-radio-group>
      </div>

      <el-row :gutter="10" v-if="showType == 2" class="mb15 flex-container">
        <el-col :span="2.5">
          <div
            @click="clickAll(0, null)"
            class="nactive"
            :class="{ active: tagIndex == 0 }"
          >
            全部：{{ registerBaseVo.total || 0 }}
          </div>
        </el-col>
        <el-col :span="2.5">
          <div
            @click="clickAll(1, 1)"
            class="nactive"
            :class="{ active: tagIndex == 1 }"
          >
            待报到：{{ registerBaseVo.sureStatus1 || 0 }}
          </div>
        </el-col>
        <el-col :span="2.5">
          <div
            @click="clickAll(2, 2)"
            class="nactive"
            :class="{ active: tagIndex == 2 }"
          >
            已报到：{{ registerBaseVo.sureStatus2 || 0 }}
          </div>
        </el-col>
        <el-col :span="2.5">
          <div
            @click="clickAll(3, 3)"
            class="nactive"
            :class="{ active: tagIndex == 3 }"
          >
            已过期：{{ registerBaseVo.sureStatus3 || 0 }}
          </div>
        </el-col>
      </el-row>

      <el-row v-if="showType == 2" :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" size="mini" @click="qrCodeOpen"
            >报到二维码</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            class="buttonT"
            size="mini"
            @click="guideOpen"
            >设置报到须知</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-form
      class="box-container"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="" style="width: 160px" prop="registerDeptId">
        <el-select
          v-model="queryParams.registerDeptId"
          placeholder="请选择进修科目"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in registerDeptList"
            :key="index"
            :label="item.registerDeptName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label=""
        style="width: 160px"
        prop="registerTypeId"
        v-if="false"
      >
        <el-select
          v-model="queryParams.registerTypeId"
          placeholder="请选择招生类型"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in registerTypeList"
            :key="index"
            :label="item.registerTypeName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" style="width: 160px" prop="batchName">
        <el-select
          v-model="queryParams.batchName"
          placeholder="请选择招生批次"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in registerBatchList"
            :key="index"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" style="width: 160px" prop="registerYear">
        <el-date-picker
          v-model="queryParams.registerYear"
          type="year"
          style="width: 160px"
          placeholder="请选择招生年份"
          format="yyyy"
          value-format="yyyy"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label=""
        style="width: 160px; margin-left: 20px"
        prop="userName"
      >
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="box-container">
      <div v-if="showType == 1" class="custom-table">
        <div class="custom-dept" v-for="(row, index) in dataList" :key="index">
          <div class="colw title">{{ row.registerDeptName }}</div>
          <div class="colw flex">
            <div class="col">
              {{ row.total }}
              <div class="desc">录取总数</div>
            </div>

            <div class="col">
              <span class="red">{{ row.sureStatus1 }}</span>
              <div class="desc">待报到</div>
            </div>
            <div class="col">
              {{ row.sureStatus2 }}
              <div class="desc">已报到</div>
            </div>
            <div class="col">
              {{ row.sureStatus3 }}
              <div class="desc">已过期</div>
            </div>
          </div>

          <div class="colw func" @click="viewOpen(row)">
            <el-button size="small" type="primary">查看名单</el-button>
          </div>
        </div>
      </div>
      <div v-if="showType == 2" class="custom-table">
        <div
          class="custom-register"
          v-for="(row, index) in dataList"
          :key="index"
        >
          <div class="border" style="display: flex; flex: 8">
            <div>
              <div class="user-name">
                <div style="margin-right: 10px">
                  <el-checkbox
                    v-model="row.check"
                    style="transform: scale(1.2)"
                    @change="handleSelectionChange($event, index)"
                  />
                </div>
                <span
                  style="
                    font-size: 18px;
                    font-weight: bolder;
                    color: #a52529;
                    cursor: pointer;
                  "
                  @click="detailOpen(row)"
                  >{{ row.userName }}</span
                >
                <el-tag type="success" v-if="row.sureStatusName == '已报到'">{{
                  row.sureStatusName
                }}</el-tag>
                <el-tag
                  type="danger"
                  v-if="row.sureStatusName == '待报到'"
                  size="small"
                  color="#eaa744"
                  style="
                    margin-left: 10px;
                    color: #fff;
                    border: 1px solid #f5a623;
                  "
                  >{{ row.sureStatusName }}</el-tag
                >

                <el-tag
                  type="info"
                  v-if="row.sureStatusName == '已过期'"
                  size="small"
                  color="#fff"
                  style="
                    margin-left: 10px;
                    color: #f5a623;
                    border: 1px solid #f5a623;
                  "
                  >延期未确认</el-tag
                >
                <div style="margin-left: auto; font-size: 12px; color: #999">
                  工号：{{ row.jobNumber }}
                </div>
              </div>
              <div class="info">
                <div class="flexInfo">
                  <div>进修科目：{{ row.registerDeptName }}</div>
                  <div style="display: flex">
                    进修期限：
                    <span v-if="row.registerNoticeVo"
                      >{{ row.registerNoticeVo.startDate }} -
                      {{ row.registerNoticeVo.endDate }}</span
                    >
                    <div v-if="row.durationName">
                      （{{ row.durationName }}）
                    </div>
                  </div>
                  <div>申请单位：{{ row.unitHospitalName }}</div>
                </div>
                <div class="flexInfo">
                  <div>
                    报到时间：{{
                      row.registerNoticeVo && row.registerNoticeVo.sureTime
                        ? parseTime(row.registerNoticeVo.sure时间戳)
                        : ""
                    }}
                  </div>
                  <div>学习完成度：{{ row.progress }}%</div>

                  <div style="display: flex" v-if="row.registerNoticeVo">
                    报到备注：
                    <div style="display: flex">
                      <div>
                        {{
                          row.registerNoticeVo &&
                          row.registerNoticeVo.sureRemark
                        }}
                      </div>
                      <div
                        @click="sureRemarkOpen(row)"
                        class="systemColor"
                        style="cursor: pointer"
                      >
                        <i class="el-icon-edit"></i>
                        <span>修改</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div
            class="border"
            style="
              width: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              position: relative;
            "
          >
            <i
              class="el-icon-delete systemColor"
              @click="handleDelete(row)"
              style="
                cursor: pointer;
                position: absolute;
                top: 10px;
                right: 10px;
                font-size: 20px;
              "
            ></i>
            <div class="flex col">
              <div class="item-center" style="display: flex">
                <img
                  src="../../../../assets/images/print.png"
                  alt=""
                  style="width: 20px; height: 20px"
                />
                <span style="color: #999">报到打卡</span>
              </div>
              <el-tag
                type="success"
                v-if="row.sureStatusName == '待报到'"
                size="small"
                @click="duty(row)"
                style="
                  margin-left: 10px;
                  color: #fff;
                  cursor: pointer;
                  background-color: #409eff;
                "
                >手动报到</el-tag
              >
              <div v-if="row.sureStatus == 2">
                报到时间：{{ parseTime(row.registerNoticeVo.sureTime) }}
              </div>
              <span>{{ row.sureStatusName }}</span>
            </div>
          </div>
        </div>
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog
      title="报名详情"
      :visible.sync="detailShow"
      width="1040px"
      append-to-body
      :close-on-click-modal="false"
    >
      <registerInfoDetailInfo
        v-if="detailShow"
        :register-id="registerInfo.id"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailShow = false">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="报到二维码"
      :visible.sync="qrCodeShow"
      width="400px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div style="text-align: center">
        <img
          v-if="qrCodeShow"
          :src="baseURL + '/business/registerBase/genQrCode'"
        />
        <div>扫码提示：手机扫码进行报到</div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qrCodeShow = false">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="设置报到须知"
      :visible.sync="guideShow"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="guideForm"
        :model="guideForm"
        :rules="guideRules"
        label-position="top"
        label-width="80px"
      >
        <el-form-item label="进修人员报到流程" prop="registerFlow">
          <el-input
            v-model="guideForm.registerFlow"
            type="textarea"
            :rows="5"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="进修人员请假流程" prop="registerLeave">
          <el-input
            v-model="guideForm.registerLeave"
            :rows="5"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="进修人员离院流程" prop="registerClose">
          <el-input
            v-model="guideForm.registerClose"
            :rows="5"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="guideSubmit">确 定</el-button>
        <el-button @click="guideShow = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="修改备注"
      :visible.sync="sureRemarkShow"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="sureRemarkForm"
        :model="sureRemarkForm"
        :rules="sureRemarkRules"
        label-width="80px"
      >
        <el-form-item label="报到备注" prop="sureRemark">
          <el-input
            type="textarea"
            :rows="5"
            v-model="sureRemarkForm.sureRemark"
            placeholder="请输入报到备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureRemarkSubmit">确 定</el-button>
        <el-button @click="sureRemarkShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryByCountRegisterBase,
  queryByDeptRegisterBase,
} from "@/api/business/registerBase";
import { listAllRegisterDept, punchCard } from "@/api/business/registerDept";
import { delRegisterInfo, listRegisterInfo } from "@/api/business/registerInfo";

import { updateRegisterNotice } from "@/api/business/registerNotice";
import { listAllRegisterType } from "@/api/business/registerType";
import { getSureInfo, updateSureInfo } from "@/api/business/sureInfo";
import { setupBatchData } from "@/utils/batchUtils";
import registerInfoDetailInfo from "../audit/components/registerInfoDetailInfo";

export default {
  name: "sure",
  components: {
    registerInfoDetailInfo,
  },
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招生信息表格数据
      dataList: [],
      //全选
      AllCheck: false,
      detailShow: false, // 报到详情
      registerInfo: {
        id: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeStatus: 3,
        payNoRefund: 1,
        sureStatus: null,
        registerDeptId: null,
        registerTypeId: null,
        batchName: null,
        registerYear: null,
        userName: null,
      },
      registerDeptList: [],
      registerTypeList: [],
      registerBatchList: [],
      registerBaseVo: {},
      // 页面显示
      showType: 1,
      // 报到二维码
      qrCodeShow: false,
      // 设置报到须知
      guideShow: false,
      guideForm: {},
      guideRules: {},
      // 填写备注
      sureRemarkShow: false,
      sureRemarkForm: {},
      sureRemarkRules: {
        sureRemark: [
          { required: true, message: "请输入报到备注", trigger: "blur" },
        ],
      },
      tagIndex: 0,
    };
  },
  async created() {
    await this.getbatch();
    this.initData();
    this.listAllRegisterDept();
    this.listAllRegisterType();
  },
  methods: {
    initData() {
      this.getCount();
      this.getList();
    },
    async getbatch() {
      await setupBatchData(this);
    },
    //打卡
    duty(data) {
      // 这里可以添加打卡逻辑，例如调用API或者更新状态
      console.log(data);
      // 假设有一个API叫做 punchCard
      punchCard(data.registerNoticeVo).then((response) => {
        this.$modal.msgSuccess("打卡成功");
        this.getList();
      });
    },
    clickAll(index, value) {
      this.canselAllcheck();
      this.tagIndex = index;
      this.queryParams.sureStatus = value;
      this.getList();
    },
    listAllRegisterDept() {
      listAllRegisterDept().then((response) => {
        this.registerDeptList = response.data;
      });
    },
    listAllRegisterType() {
      listAllRegisterType().then((response) => {
        this.registerTypeList = response.data;
      });
    },
    getCount() {
      queryByCountRegisterBase(this.queryParams).then((response) => {
        if (response.data) {
          this.registerBaseVo = response.data;
          this.registerBaseVo.total =
            this.registerBaseVo.sureStatus1 +
            this.registerBaseVo.sureStatus2 +
            this.registerBaseVo.sureStatus3;
        }
      });
    },
    getList() {
      this.loading = true;
      if (this.showType == 1) {
        queryByDeptRegisterBase(this.queryParams).then((response) => {
          this.dataList = response.rows;
          for (let i = 0; i < this.dataList.length; i++) {
            this.dataList[i].total =
              this.dataList[i].sureStatus1 +
              this.dataList[i].sureStatus2 +
              this.dataList[i].sureStatus3;
          }
          this.total = response.total;
          this.loading = false;
        });
      } else if (this.showType == 2) {
        listRegisterInfo(this.queryParams).then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 报到二维码
    qrCodeOpen() {
      this.qrCodeShow = true;
    },
    // 设置报到须知
    guideOpen() {
      getSureInfo(1).then((response) => {
        this.guideForm = response.data;
        this.guideShow = true;
      });
    },
    guideSubmit() {
      this.$refs["guideForm"].validate((valid) => {
        if (valid) {
          updateSureInfo(this.guideForm).then((response) => {
            this.$modal.msgSuccess("更新成功");
            this.initData();
            this.guideShow = false;
          });
        } else {
          this.$modal.msgError("请完善表单内容");
        }
      });
    },
    // 修改备注
    sureRemarkOpen(row) {
      this.sureRemarkForm = {
        id: row.registerNoticeVo.id,
        sureRemark: row.registerNoticeVo.sureRemark,
      };
      this.sureRemarkShow = true;
    },
    sureRemarkSubmit() {
      this.$refs["sureRemarkForm"].validate((valid) => {
        if (valid) {
          updateRegisterNotice(this.sureRemarkForm).then((response) => {
            this.$modal.msgSuccess("修改完成");
            this.initData();
            this.sureRemarkShow = false;
          });
        } else {
          this.$modal.msgError("请完善表单内容");
        }
      });
    },
    // 查看名单
    viewOpen(row) {
      this.showType = 2;
      this.queryParams.registerDeptId = row.registerDeptId;
      this.handleQuery();
    },
    // 删除
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报名信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRegisterInfo(ids);
        })
        .then(() => {
          this.initData();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 切换视图
    showTypeChange() {
      this.ids = [];
      this.single = true;
      this.multiple = true;
      this.queryParams.registerDeptId = null;
      this.queryParams.userName = null;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.initData();
      this.canselAllcheck();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // 手动重置批次和年份为空
      this.queryParams.batchName = null;
      this.queryParams.registerYear = null;
      this.handleQuery();
      this.canselAllcheck();
    },
    //取消全选
    canselAllcheck() {
      this.AllCheck = false;
      this.ids = [];
      this.single = true;
      this.multiple = true;
    },
    // 全选
    handleSelectionChangeAll(checked) {
      let ids = [];
      for (let i = 0; i < this.dataList.length; i++) {
        // 使用 $set 确保属性被响应式地修改
        this.$set(this.dataList, i, { ...this.dataList[i], check: checked });
        if (checked) {
          ids.push(this.dataList[i].id);
        }
      }
      this.ids = ids;
      this.single = ids.length !== 1;
      this.multiple = !ids.length;
      console.log(this.multiple);
      console.log(this.single);
    },
    detailOpen(row) {
      this.registerInfo.id = row.id;
      this.detailShow = true;
    },
    // 多选框选中数据
    handleSelectionChange(checked, index) {
      console.log(checked, index);
      // 使用 $set 确保属性被响应式地修改
      this.$set(this.dataList, index, {
        ...this.dataList[index],
        check: checked,
      });

      let ids = [];
      for (let i = 0; i < this.dataList.length; i++) {
        if (this.dataList[i].check) {
          ids.push(this.dataList[i].id);
        }
      }
      this.ids = ids;
      this.single = ids.length !== 1;
      this.multiple = !ids.length;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 0 20px 20px 20px;
}

.user-name {
  display: flex;
  align-items: center;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  /* 控制列之间的间距 */
  background: #eeeff4;
  padding: 20px 0;
}

.box-container {
  padding: 0;
  padding-left: 20px;
}

::v-deep .el-radio-button__orig-radio + .el-radio-button__inner {
  width: 140px;
}

::v-deep .el-radio-button__orig-radio:checked + .el-radio-button__inner {
  width: 140px;
}

.custom-table {
  .custom-dept {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid #d8d8d8;
    padding: 0 20px;
    height: 90px;
    margin-bottom: 20px;
    text-align: center;
    font-size: 18px;
    font-weight: bold;

    .col {
      width: calc(100% / 4);
      // font-weight: bolder;

      .desc {
        font-size: 12px;
        color: #888;
        margin-top: 10px;
        font-weight: normal;
      }
    }

    .title {
      font-size: 18px;
      text-align: left;
      color: #000;
    }

    .func {
      font-size: 18px;
      color: #a52529;
      cursor: pointer;
    }
  }

  .custom-dept:hover {
    border: 1px solid red;
  }

  .custom-register {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // border: 1px solid rgb(232, 232, 232);
    // padding: 10px;
    // text-align: center;
    font-size: 14px;
    margin-bottom: 10px;
    height: 150px;

    .info {
      display: flex;
      justify-content: space-between;
      color: #606266;

      .flexInfo {
        display: flex;
        flex-direction: column;
        margin-top: 10px;
        font-size: 14px;
        width: 360px;
        line-height: 20px;
        flex: 1;
      }
    }

    .col {
      .desc {
        color: #666666;
        margin-top: 10px;
        font-weight: normal;
      }
    }

    .col:nth-child(1) {
      width: calc(100% - 600px);
      text-align: left;
    }

    .col:nth-child(2) {
      width: 220px;
      font-size: 12px;
    }

    .col:nth-child(3) {
      width: 200px;
    }

    .col:nth-child(4) {
      width: 200px;
    }

    .col:nth-child(5) {
      width: 200px;
    }
  }
}
</style>
